﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Data;

namespace NJL.WebManage.SystemNotice
{
    public partial class ManageNotice : BasePage
    {
        PagedDataSource pds;
        NJL.BLL.NJL_SYS_ANNOUNCEMENT bll = new BLL.NJL_SYS_ANNOUNCEMENT();
        NJL.Model.NJL_SYS_ANNOUNCEMENT model = new Model.NJL_SYS_ANNOUNCEMENT();
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (WEBSITE_CODE == "1")
                {
                    DataSet ds = bllArea.GetList("AREA_CODE like '%0000'");
                    ddlarea.DataSource = ds;
                    ddlarea.DataTextField = "AREA_NAME";
                    ddlarea.DataValueField = "AREA_CODE";
                    ddlarea.DataBind();
                }
                else
                {
                    dispArea.Visible = false;
                }
                ViewState["pager"] = "0";
                bind();

            }
        }
        public void bind()
        {
            DataSet ds = bll.GetList(sqlBind());
            txtPage.Text = (Convert.ToInt32(ViewState["pager"]) + 1).ToString();
            pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 15;
            pds.CurrentPageIndex = Convert.ToInt32(ViewState["pager"]);
            ViewState["endpage"] = pds.PageCount;
            lbtnFirst.Enabled = true;
            lbtnPrev.Enabled = true;
            lbtnNext.Enabled = true;
            lbtnEnd.Enabled = true;
            if (ViewState["pager"].ToString() == "0")
            {
                lbtnFirst.Enabled = false;
                lbtnPrev.Enabled = false;
            }
            if (Convert.ToInt32(ViewState["pager"]) == pds.PageCount - 1)
            {
                lbtnNext.Enabled = false;
                lbtnEnd.Enabled = false;
            }

            lit.Text = ds.Tables[0].Rows.Count.ToString();
            litAllPage.Text = pds.PageCount.ToString();
            litCurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();
            rptUserList.DataSource = pds;
            rptUserList.DataBind();
        }
        string sql;
        protected string sqlBind()
        {
            if (WEBSITE_CODE == "1")
            {
                if (ddlarea.SelectedValue == "000000")
                {
                    sql = "ANNOUNCEMENT_NAME like '%" + txtKeywords.Text.Trim() + "%'";
                }
                else
                {
                    sql = "NJL_SYS_BASE_ID='" + ddlarea.SelectedValue + "' and ANNOUNCEMENT_NAME like '%" + txtKeywords.Text.Trim() + "%'";
                }
            }
            else
            {
                sql = "NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and ANNOUNCEMENT_NAME like '%" + txtKeywords.Text.Trim() + "%'";
            }
            return sql;
        }
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                string id = e.CommandArgument.ToString();
                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
            else if (e.CommandName == "imgbtn3")
            {
                string id = e.CommandArgument.ToString();
                model = bll.GetModel(id);
                if (model != null)
                {
                    if (model.IS_PUBLISH == 1)
                    {
                        model.IS_PUBLISH = 0;
                    }
                    else
                    {
                        model.IS_PUBLISH = 1;
                        model.PUBLISH_DATE_TIME = DateTime.Now;
                    }
                }
                model.ID = id;
                if (bll.Update(model))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "设为发布状态失败");
                }
            }
        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litNum") as Literal;
                lit.Text = ((e.Item.ItemIndex + 1) + (Convert.ToInt32(ViewState["pager"]) + 1) * 15 - 15).ToString();

                ImageButton imgbtn3 = e.Item.FindControl("imgbtn3") as ImageButton;
                if (imgbtn3.AlternateText == "1")
                {
                    imgbtn3.ImageUrl = "~/Images/true.gif";
                }
                else if (imgbtn3.AlternateText == "0")
                {
                    imgbtn3.ImageUrl = "~/Images/false.gif";
                }
            }
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();

        }
        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) - 1;
            bind();

        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = Convert.ToInt32(ViewState["pager"]) + 1;
            bind();
        }
        protected void lbtnEnd_Click(object sender, EventArgs e)
        {

            ViewState["pager"] = Convert.ToInt32(ViewState["endpage"]) - 1;
            bind();

        }
        protected void btnDel_Click1(object sender, EventArgs e)
        {
            string[] cbxid = HSelectId.Value.Split(',');
            foreach (string id in cbxid)
            {

                if (bll.Delete(id))
                {
                    bind();
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "删除失败");
                }
            }
        }
        private static Regex RegNumber = new Regex("^[0-9]+$");
        protected void btnRir_Click(object sender, EventArgs e)
        {
            Match mc = RegNumber.Match(txtPage.Text);
            if (mc.Success && txtPage.Text != "0")
            {
                ViewState["pager"] = Convert.ToInt32(txtPage.Text) - 1;
                bind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["pager"] = "0";
            bind();
        }
    }
}